import 'package:common_utils/common_utils.dart';
import 'package:flutter_demo/tmac/util/AndroidFlutterPlugin.dart';
import 'package:geolocator/geolocator.dart';

class LocationUtil {
  double latitude;
  double longitude;
  Geolocator geolocator;

  factory LocationUtil() => _getInstance();

  static LocationUtil get instance => _getInstance();

  static LocationUtil _instance;

  LocationUtil._internal() {
    geolocator = Geolocator()..forceAndroidLocationManager;
  }

  static LocationUtil _getInstance() {
    if (_instance == null) {
      _instance = new LocationUtil._internal();
    }
    return _instance;
  }

  Future<dynamic> getCityName() async {
    bool isOpen = await geolocator.isLocationServiceEnabled();
    Position position = await geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.best);

    List<Placemark> address = await geolocator.placemarkFromCoordinates(
        position.latitude, position.longitude);
    Placemark placemark = address[0];
    print("${placemark.locality} ${placemark.name} ${placemark.subLocality}");
    return placemark == null ? "" : placemark.name;
  }
}
